/**
 * 
 */
package soa.security.web.export;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import soa.security.domain.LdapTree;
import v1.web.export.DefaultFormatExport;

/**
 * @author Cre.Gu
 *
 */
public class LdapTreeJsonFormatExport extends
		DefaultFormatExport<Collection<LdapTree>, JSONObject> {

	public LdapTreeJsonFormatExport(Collection<LdapTree> source) {
		super(source);
	}

	@Override
	public JSONObject export() {
		Map<String, JSONObject> map = new HashMap<String, JSONObject>();
		JSONObject rootJson = new JSONObject();

		wrapRootInfo(rootJson);
		map.put("-1", rootJson);

		for (LdapTree ldapTree : source) {
			JSONObject currentJson = map.get(ldapTree.getId());

			if (currentJson == null)
				currentJson = new JSONObject();
			exportInfo(ldapTree, currentJson);

			if (ldapTree.getRegion() != null) {
				String id = ldapTree.getRegion().getDn().toString();
				getParentAddChild(map, id, currentJson);
			}

			if (ldapTree.getOrganization() != null) {
				String id = ldapTree.getOrganization().getDn().toString();
				getParentAddChild(map, id, currentJson);
			}

			if (ldapTree.getDepartment() != null) {
				String id = ldapTree.getDepartment().getDn().toString();
				getParentAddChild(map, id, currentJson);
			}

			if (!ldapTree.hasParent()) {
				JSONObject parentJson = rootJson;
				addChild(parentJson, currentJson);
			}

			map.put(ldapTree.getId(), currentJson);
		}

		return rootJson;
	}

	protected void wrapRootInfo(JSONObject parentJson) throws JSONException {
		parentJson.put("title", "根节点");
		parentJson.put("text", "根节点");
		parentJson.put("name", "root");
		parentJson.put("value", "");
		parentJson.put("id", -1);
	}

	protected void exportInfo(LdapTree ldapTree, JSONObject currentJson)
			throws JSONException {

		currentJson.put("id", ldapTree.getId());
		currentJson.put("bid", ldapTree.getBid());
		
		currentJson.put("text", ldapTree.getText());
		currentJson.put("organizationId", ldapTree.getOrganizationId());

		currentJson.put("isLeaf", true);
		currentJson.put("checked_", ldapTree.isChecked());
//		if (ldapTree.getRegion() != null)
//			currentJson.put("expanded", true);

		currentJson.put("iconCls", ldapTree.getIconCls());
		currentJson.put("className", ldapTree.getClassName());
		currentJson.put("type", ldapTree.getType());
		currentJson.put("regionLevel", ldapTree.getRegionLevel());

		JSONArray array = currentJson.optJSONArray("children");

		if (array != null) {
			currentJson.put("isLeaf", false);
		}
	}

	protected void getParentAddChild(Map<String, JSONObject> map, String id,
			JSONObject currentJson) {
		JSONObject parentJson = map.get(id);
		if (parentJson == null) {
			parentJson = new JSONObject();
			parentJson.put("id", id);
			map.put(id, parentJson);
		}

		addChild(parentJson, currentJson);
	}

	protected void addChild(JSONObject parentJson, JSONObject currentJson)
			throws JSONException {
		JSONArray array = parentJson.optJSONArray("children");

		if (array == null) {
			array = new JSONArray();
			parentJson.put("children", array);
		}

		parentJson.put("isLeaf", false);
		array.put(currentJson);
	}
}
